﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class OplataInfForm : Form
    {
        string caption = "Информация о договоре";

        Contract contract;
        Oplata oplata; 

        DataSet ds;

        public OplataInfForm()
        {
            InitializeComponent();
        }

        int id_contract;

        public OplataInfForm(Form parentForm, int id_contract)
        {
            InitializeComponent();

            ds = new DataSet();
            oplata = new Oplata();
            contract = new Contract();

            this.id_contract = id_contract;

            this.Text = caption;
        }

        private void UpdateForm()
        {
            Tools.RunWithProgressIndicator(this, delegate
            {
                this.contract.GetInfo(id_contract, ref ds);
            });


            if (ds.Tables.Count > 0)
            {
                lblConsultant.Text = ds.Tables[0].Rows[0]["Консультант"].ToString();
                lblContractNumber.Text = ds.Tables[0].Rows[0]["Номер договора"].ToString();
                lblContragent.Text = ds.Tables[0].Rows[0]["Контрагент"].ToString();
                lblOplacheno.Text = ds.Tables[0].Rows[0]["Оплачено"].ToString();
                lblOplataType.Text = ds.Tables[0].Rows[0]["Тип оплаты"].ToString();
                lblOstatok.Text = ds.Tables[0].Rows[0]["Остаток"].ToString();
                lblRezident.Text = ds.Tables[0].Rows[0]["Резидент"].ToString();
                lblSumItog.Text = ds.Tables[0].Rows[0]["Сумма договора"].ToString();
                lblSumPre.Text = ds.Tables[0].Rows[0]["Аванс"].ToString();
                lbOplataRaz.Text = ds.Tables[0].Rows[0]["Оплачено раз"].ToString();

                DateTime dt;

                DateTime.TryParse(ds.Tables[0].Rows[0]["Срок оплаты"].ToString(), out dt);
                if (dt.Year > 2000)
                {
                    label6.Text = dt.ToString("dd.MM.yyyy");
                }

                DateTime.TryParse(ds.Tables[0].Rows[0]["Дата заключения"].ToString(), out dt);
                if (dt.Year > 2000)
                {
                    lblDateZakl.Text = dt.ToString("dd.MM.yyyy");
                }

                //if (decimal.Parse(lblOstatok.Text) > 0)
                //{
                //    btnAdd.Enabled = true;
                //}
                //else
                //{
                //    btnAdd.Enabled = false;
                //}
            }

            dataGridViewTable.DataSource = ds.Tables[1];
            dataGridViewTable.Columns[0].Visible = false;
            dataGridViewTable.Columns[1].Visible = false;

            dataGridViewTable.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void OplataInfForm_Load(object sender, EventArgs e)
        {
            UpdateForm();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Oplata oplata = new Oplata();
            decimal d = 0;

            Tools.RunWithProgressIndicator(this, delegate
            {

                string contractNumber = dataGridViewTable.Rows[dataGridViewTable.SelectedCells[0].RowIndex].Cells["id_contract"].Value.ToString();
                string[] tmp = contractNumber.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

                oplata.Definition = string.Format("оплата по договору {0} от {1}",
                    contractNumber,
                    tmp[0]);

                oplata.id_contract = int.Parse(dataGridViewTable.Rows[dataGridViewTable.SelectedCells[0].RowIndex].Cells["id_contract"].Value.ToString());

                d = decimal.Parse(lblOstatok.Text);

                oplata.contract.SelectById(oplata.id_contract);
            });

            OplataForm oplataForm = new OplataForm(oplata, d);

            oplataForm.ShowDialog();

            UpdateForm();
        }

        Microsoft.Office.Interop.Word.Application wrd;

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridViewTable_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
        {           
                dataGridViewTable.Rows[e.RowIndex].Selected = true;
                e.ContextMenuStrip = contextMenuStrip1;
            
        }

        /// <summary>
        /// Просмотр оплаты в Word
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void открытьВWordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OplataForm oplataFrm = new OplataForm();

            oplataFrm.ViewOplata((int)dataGridViewTable.Rows[dataGridViewTable.SelectedCells[0].RowIndex].Cells["id_oplata"].Value);
        }
    }
}
